/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vng.survey.controller;

import com.vng.zing.survey.thrift.client.SurveyClient;
import com.vng.zing.survey.thrift.model.ShortInfoSurveyItem;
import com.vng.zing.survey.thrift.model.UserType;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static vng.survey.model.CommonModel.OFFSET;
import vng.survey.model.CommonModel;

/**
 *
 * @author namhh
 */
public class AjaxHome extends BaseController {

//    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
//            throws ServletException, IOException, Exception {
//
//        String content = "";
//        Integer page = Integer.parseInt(request.getParameter("page"));
//        page--;
//        Integer idUser = Integer.parseInt(request.getParameter("idUser"));
//        String type = request.getParameter("type");
//        if ("NEW".equals(type)) {
//            SurveyClient surveyClient = SurveyClient.getInstance();
//            List<ShortInfoSurveyItem> newestSurvey = surveyClient.getListShortInfoSurveyItem(idUser, page, OFFSET);
//            TemplateDataDictionary dicNewTable = TemplateDictionary.create();
//
//            for (ShortInfoSurveyItem t : newestSurvey) {
//                TemplateDataDictionary slideItems = dicNewTable.addSection("SLICE_SHOW_ITEMS");
//                slideItems.setVariable("RESPONSE_LINK", "response?id=" + t.getIdSurvey());
//                slideItems.setVariable("TITLE", t.getTitle());
//                slideItems.setVariable("VIEW", String.format("%s", t.getView()));
//                slideItems.setVariable("RESPONSE", String.format("%s", t.getResponse()));
//                slideItems.setVariable("LINK", "stat?id=" + t.getIdSurvey());
//            }
//
//            content = CommonModel.getNewTable(dicNewTable);
//        }
//
//        if ("HOT".equals(type)) {
//            SurveyClient surveyClient = SurveyClient.getInstance();
//            List<ShortInfoSurveyItem> newestSurvey = surveyClient.getListShortInfoSurveyItem(idUser, 0, 0);
//            TemplateDataDictionary dicNewTable = TemplateDictionary.create();
//
//            for (ShortInfoSurveyItem t : newestSurvey) {
//                TemplateDataDictionary slideItems = dicNewTable.addSection("SLICE_SHOW_ITEMS");
//                slideItems.setVariable("RESPONSE_LINK", "response?id=" + t.getIdSurvey());
//                slideItems.setVariable("TITLE", t.getTitle());
//                slideItems.setVariable("VIEW", String.format("%s", t.getView()));
//                slideItems.setVariable("RESPONSE", String.format("%s", t.getResponse()));
//                slideItems.setVariable("LINK", "stat?id=" + t.getIdSurvey());
//            }
//
//            content = CommonModel.getHotTable(dicNewTable);
//        }
//
//        response.setContentType("text/plain");
//        response.setCharacterEncoding("UTF-8");
//        response.getWriter().print(content);
//
//    }
    @Override
    public String render(HttpServletRequest request, HttpServletResponse resp) throws IOException, Exception {
        String content = "";
        Integer page = Integer.parseInt(request.getParameter("page"));
        page--;
        String type = request.getParameter("type");
        if ("NEW".equals(type)) {

            SurveyClient surveyClient = SurveyClient.getInstance();
            List<ShortInfoSurveyItem> newestSurvey;
            newestSurvey = surveyClient.getListShortInfoSurveyItem(1, page, CommonModel.OFFSET, UserType.USER);
            if (request.getParameter("by") != null && request.getParameter("asc") != null) {
                String by = request.getParameter("by");
                boolean asc = Boolean.parseBoolean(request.getParameter("asc"));
                sort(newestSurvey, by, asc);
            }

            TemplateDataDictionary dicNewTable = TemplateDictionary.create();

            for (ShortInfoSurveyItem t : newestSurvey) {
                TemplateDataDictionary slideItems = dicNewTable.addSection("SLICE_SHOW_ITEMS");
                slideItems.setVariable("RESPONSE_LINK", "response?id=" + t.getIdSurvey());
                slideItems.setVariable("TITLE", t.getTitle());
                slideItems.setVariable("VIEW", String.format("%s", t.getView()));
                slideItems.setVariable("RESPONSE", String.format("%s", t.getResponse()));
                slideItems.setVariable("LINK", "stat?id=" + t.getIdSurvey());
            }

            content = CommonModel.getNewTable(dicNewTable);
        }

        if ("HOT".equals(type)) {
            SurveyClient surveyClient = SurveyClient.getInstance();
            List<ShortInfoSurveyItem> newestSurvey = surveyClient.getListShortInfoSurveyItemHost(1, page, OFFSET);
            TemplateDataDictionary dicNewTable = TemplateDictionary.create();
             if (request.getParameter("by") != null && request.getParameter("asc") != null) {
                String by = request.getParameter("by");
                boolean asc = Boolean.parseBoolean(request.getParameter("asc"));
                sort(newestSurvey, by, asc);
            }

            for (ShortInfoSurveyItem t : newestSurvey) {
                TemplateDataDictionary slideItems = dicNewTable.addSection("SLICE_SHOW_ITEMS");
                slideItems.setVariable("RESPONSE_LINK", "response?id=" + t.getIdSurvey());
                slideItems.setVariable("TITLE", t.getTitle());
                slideItems.setVariable("VIEW", String.format("%s", t.getView()));
                slideItems.setVariable("RESPONSE", String.format("%s", t.getResponse()));
                slideItems.setVariable("LINK", "stat?id=" + t.getIdSurvey());
            }

            return CommonModel.getHotTable(dicNewTable);
        }

        return content;
    }

    private void sort(List<ShortInfoSurveyItem> item, String by, boolean asc) { List<ShortInfoSurveyItem> infoSurveyItem = new ArrayList<>(item.size());
        for(int i=0;i<item.size()-1;i++){
            for(int j=i+1;j<item.size();j++){
                if("VIEW".equals(by)){
                    if(asc && item.get(i).getView()>item.get(j).getView()){
                        swap(item, i, j);
                    }
                     if(!asc && item.get(i).getView()<item.get(j).getView()){
                        swap(item, i, j);
                    }
                    
                }
                if("RES".equals(by)){
                     if(asc && item.get(i).getResponse()>item.get(j).getResponse()){
                        swap(item, i, j);
                    }
                     if(!asc && item.get(i).getResponse()<item.get(j).getResponse()){
                        swap(item, i, j);
                    }
                }
                
                
            }
        }
        
    }
    void swap(List<ShortInfoSurveyItem> item, int i,int j){
        ShortInfoSurveyItem temp = item.get(i);
        item.set(i, item.get(j));
        item.set(j, temp);
    }
    
    
}
